import random
def get_random(rate):
    i = random.randint(-1000000000,100000000)%100
    if i<(100*rate):
        return 1
    else:
        return 0

def init(people,rate):
    a=[]     #投票结果
    for i in range(people):
        a.append(get_random(rate[i]))    #根据比例生成随机数
    return a

#score,初始化的score
#scores，计算的所有人的score
#rate，投票率，a：投票结果
def judge_score(scores, score, people, a, rate):
    th = 0.8
    for i in range(len(scores)):
        if scores[i]<score*th:
            people = people - 1
            del(a[i])
            del(rate[i])
            del(scores[i])
            break
    return scores, people, a, rate

def vote(Temptation, people, round, growth, score):
    scores = [score] * people
    rate = [Temptation]*people     #初始每个人的投票率
    a=init(people,rate)    #第一轮投票
    result = []    #要输出的所有投票率
    for i in range(len(a)):
        if a[i] == 1:  # 投票
            rate[i] = rate[i] + growth
            scores[i] = scores[i] + growth * 100
        else:
            rate[i] = rate[i] - growth
            scores[i] = scores[i] - growth * 100

    scores, people, a, rate=judge_score(scores, score, people, a, rate)   #标记的score低于阈值的people列表

    good_score = 0
    bad_score = 0

    for n in range(len(scores)):
        if a[n] == 1:   #投票
            good_score = good_score + scores[n]
        else:
            bad_score = bad_score + scores[n]

    result.append((good_score/a.count(1))/(good_score/a.count(1)+bad_score/a.count(0)))

    for i in range(1,round):
        for i in range(people):
            if a[i] == 1:  # 投票
                rate[i] = rate[i] + growth
                scores[i] = scores[i] + growth * 100
            else:
                rate[i] = rate[i] - growth
                scores[i] = scores[i] - growth * 100

        a = init(people,rate)   #投票

        good_score = 0
        bad_score = 0

        for n in range(len(scores)):
            if a[n] == 1:  # 投票
                good_score = good_score + scores[n]
            else:
                bad_score = bad_score + scores[n]
        result.append((good_score / a.count(1)) / (good_score / a.count(1) + bad_score / a.count(0)))
        scores, people, a, rate = judge_score(scores, score, people, a, rate)  # 小于阈值的score

    return result

if __name__ == '__main__':
    Temptation = 0.6   #投票率
    score = 100
    people = 1000
    round = 14
    growth = 0.05
    a = vote(Temptation, people, round, growth, score)
    print(a)
